#include "CenterMoveStrategy.h"
#include <GraphicsBaseItem.h>

CenterMoveStrategy::CenterMoveStrategy(GraphicsBaseItemData& data)
	:MoveStrategy(data)
{

}

void CenterMoveStrategy::horizontalLimit(const QPointF& newPos, QPointF& returnPos)
{
	const QRectF parentRect = getItemData().getParentItem()->getItemData().getScaleRect();
	const QRectF rect = getItemData().getScaleRect();
	// 获取外接矩形
	const QRectF boundingRect = GraphicsUtil::mapAnglePolygon(rect, getItemData().getRotateAngle()).boundingRect();
	const qreal rightBounding = parentRect.width();
	const qreal leftBounding = 0;

	if (leftBounding <= rightBounding) {
		returnPos.setX(qBound(leftBounding, newPos.x(), rightBounding));
	}
	else {
		returnPos.setX(qBound(rightBounding, newPos.x(), leftBounding));
	}
}

void CenterMoveStrategy::verticalLimit(const QPointF& newPos, QPointF& returnPos)
{
	const QRectF parentRect = getItemData().getParentItem()->getItemData().getScaleRect();
	const QRectF rect = getItemData().getScaleRect();
	// 获取外接矩形
	const QRectF boundingRect = GraphicsUtil::mapAnglePolygon(rect, getItemData().getRotateAngle()).boundingRect();
	const qreal bottomBounding = parentRect.height();
	const qreal topBounding = 0;
	if (bottomBounding <= topBounding) {
		returnPos.setY(qBound(bottomBounding, newPos.y(), topBounding));
	}
	else {
		returnPos.setY(qBound(topBounding, newPos.y(), bottomBounding));
	}
}
